<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 银行管理
 *
 * @author www.dingwei.cn
 */

class AdmBankModel extends MY_Model{
	
	private $tbBank = 'admin_bank';
    /**
     * 后台管理获取列表
     * @return	array
     * @author  heyu
     */
    public function getAdminPageList(){
        $status    = (int)get('status');
        $bankName    = trim(get('bank_name'));

        if(!empty($status)){
            $this->db->where(array('status' => $status));
        }
        if ($bankName){
            $this->db->like(array('bank_name' => $bankName));
        }
        $this->db->order_by(' id desc');

        $data = $this->pagination($this->tbBank);
        return $data;
    }

    /**
     * 查询
     * @author  heyu
     */
    public function getList(){
        $data = array();

        $this->db->select('id,bank_name,bank_icon');
        $this->db->order_by('id desc');
        $list = $this->getAll($this->tbBank);
        if($list){
            foreach($list as $k=>&$v){
                $v['bank_icon'] = getImage($v['bank_icon']);
            }
            $data = $list;
        }
        return $data;
    }

    /**
     *银行列表
     * @param $page
     * @return array
     * @khj
     */
    public function appGetBankList($page){

        $this->db->where('type', 1);
        $this->db->select('id,bank_name,bank_icon');

        $tA = $this->appPage($this->tbBank, $page);
        foreach ($tA['list'] as $k=>$value){
            $tA['list'][$k]['bank_icon'] = getImage($value['bank_icon'],'og');
        }

        return $tA;
    }

    /**
     * 获取详情
     * @param $id
     * @return array
     * @author  heyu
     */
    public function getInfo($id){
        $this->db->where(array('id'=>$id));
        $data = $this->getOne($this->tbBank);
        if($data){
            $data['bank_icon'] = isset($data['bank_icon']) ? getImage($data['bank_icon']) : getDefaultImg();
        }
        return $data;
    }

    /**
     * 获取
     * @param 	array $ids
     * @return	array
     * @author  heyu
     */
    public function getInfoByIds($ids){

        $data = array();
        if( ! $ids){
            return $data;
        }

        $this->db->select('id,name,img')
            ->where_in('id', $ids);
        $tA = $this->getAll($this->tbBank);

        foreach ($tA as $v){
            $v['img'] = getImage($v['img']);
            $data[$v['id']] = $v;
        }

        return $data;
    }

    /**
     * 保存信息
     * @param	array	$data	保存数据
     * @param	integer $id		用户id
     * @return	boolean
     * @author  heyu
     */
    public function save($data, $id = 0){

        if( ! $data) return false;
        if($id){
            $this->db->where(array('id' => $id));
            if($this->update($this->tbBank, $data)){
                return true;
            }
        }else{
            if($id = $this->insert($this->tbBank, $data)){
                return $id;
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return bool
     * @author  heyu
     */
    public function deleteBank($id)
    {
        if (!$id){
            return false;
        }
        $this->db->where(array('id'=>$id));
        return $this->delete($this->tbBank);
    }
}